import os

# 确保 templates 目录存在
if not os.path.exists('templates'):
    os.makedirs('templates')

# 创建 base.html
base_html = '''<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气数据系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="/">天气数据系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/dashboard">数据分析</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        {% block content %}{% endblock %}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="{{ url_for('static', filename='js/charts.js') }}"></script>
</body>
</html>
'''

# 创建 index.html
index_html = '''{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h1 class="mb-4">实时天气数据</h1>
    </div>

    {% for data in latest_data %}
    <div class="col-md-3 mb-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">{{ data.location }}</h5>
                <p class="card-text">
                    <strong>温度:</strong> {{ data.temperature }}°C<br>
                    <strong>湿度:</strong> {{ data.humidity }}%<br>
                    <strong>天气:</strong> {{ data.weather_condition }}<br>
                    <strong>风速:</strong> {{ data.wind_speed }} km/h<br>
                    <strong>风向:</strong> {{ data.wind_direction }}
                </p>
                <small class="text-muted">更新时间: {{ data.date_time }}</small>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
{% endblock %}
'''

# 写入文件
with open('templates/base.html', 'w', encoding='utf-8') as f:
    f.write(base_html)

with open('templates/index.html', 'w', encoding='utf-8') as f:
    f.write(index_html)

print("模板文件已创建完成！")